package meta.api.web.value.presentation;

import java.util.ArrayList;
import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import meta.api.web.common.util.ApiUtil;
import meta.api.web.operation.businessinfo.ValueInfo;
import meta.api.web.operation.presentation.form.ValueForm;
import meta.api.web.operation.presentation.form.ValueListResult;
import meta.api.web.value.businesscontrol.ValueMngBusiCtrl;
import meta.common.log.CmnLogger;
import meta.common.msg.MessageConst;
import meta.common.util.JsonUtil;

@Controller
@RequestMapping(value="api/value")
@Tag(name = "值管理机能", description = "值管理机能")
public class ValueMngController {

    private static final CmnLogger logger = CmnLogger.getLogger(ValueMngController.class);

    private static final String FUNC_NM = "值管理机能";

    private static final String ERROR_MSG_DATA_ERROR= "请求数据错误";
    private static final String ERROR_MSG_GET_VALUE_LIST_ERROR= "获取值列表失败";
    private static final String ERROR_MSG_GET_VALUE_ERROR= "获取值失败";

    @Autowired
    private ValueMngBusiCtrl valueMngBusiCtrl;

    /**
     * 获取值列表
     * 
     * @param req 请求
     * @param res 响应
     * @param current 当前页
     * @param pageSize 每页记录数
     * @return
     * @throws Exception 异常
     */
    @PostMapping(value="/list")
    @Operation(tags = "获取值列表", description = "获取值列表")
    public String list(HttpServletRequest req, HttpServletResponse res,
            @RequestParam(value = "current", required = false) Integer current,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {
        ApiUtil.callApiLog(req, FUNC_NM, "获取值列表", null);

        ValueForm valueForm = JsonUtil.toObj(ApiUtil.readTxtBody(req), ValueForm.class);

        if (valueForm == null || !valueForm.checkRequiredField4Usl()) {
            ApiUtil.setErrorJsonResponse(res, ERROR_MSG_DATA_ERROR);
            return null;
        }

        int total;
        List<Object> valueList= new ArrayList<Object>();
        try {
            // 获取值总件数
            total = valueMngBusiCtrl.getTotal(
                    valueForm.getType(), valueForm.getKeys());

            // 获取值列表
            if (total > 0) {
                valueList = valueMngBusiCtrl.getList(
                        valueForm.getType(), valueForm.getKeys(),
                        current, pageSize);
            }

            ValueListResult result = new ValueListResult();
            result.setTotal(total);
            result.setList(valueList);

            ApiUtil.setJsonResponse(res, JsonUtil.toJsonStr(result));
        } catch (Exception e) {
            logger.log(MessageConst.MSG_01090001, null, e);
            ApiUtil.setErrorJsonResponse(res, ERROR_MSG_GET_VALUE_LIST_ERROR);
        }

        return null;
    }

    /**
     * 获取值
     * 
     * @param req 请求
     * @param res 响应
     * @return
     * @throws Exception 异常
     */
    @PostMapping(value="/get")
    @Operation(tags = "获取值", description = "获取值")
    public String get(HttpServletRequest req, HttpServletResponse res) throws Exception {
        ApiUtil.callApiLog(req, FUNC_NM, "获取值", null);

        ValueInfo reqValue = JsonUtil.toObj(ApiUtil.readTxtBody(req), ValueInfo.class);

        if (reqValue == null || !reqValue.checkRequiredField4Get()) {
            ApiUtil.setErrorJsonResponse(res, ERROR_MSG_DATA_ERROR);
            return null;
        }

        // 获取值
        try {
            Object resValue = valueMngBusiCtrl.getValue(reqValue.getType(), reqValue.getKey());
            ApiUtil.setJsonResponse(res, JsonUtil.toJsonStr(resValue));
        } catch (Exception e) {
            logger.log(MessageConst.MSG_01090001, null, e);
            ApiUtil.setErrorJsonResponse(res, ERROR_MSG_GET_VALUE_ERROR);
        }

        return null;
    }
}
